package com.link510.aitools.core.data;


import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.Utils;
import com.link510.aitools.R;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class CWMRetrofit {

    private static CWMRetrofit mInstance = null;

    private Retrofit retrofit;

    private CWMRetrofit() {
    }


    /**
     * 初始化
     */
    public void init() {

        String baseUrl = Utils.getApp().getString(R.string.base_url);

        if (AppUtils.isAppDebug()) {
            baseUrl = Utils.getApp().getString(R.string.local_base_url);
        }

        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(genericClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();


//        //1.创建Retrofit实例对象
//
//        //设置服务器主机地址,要求url必须以/结尾
//        //使用Gson作为json数据的转换器
//        retrofit = new Retrofit.Builder().client(genericClient())
//
//                //设置服务器主机地址,要求url必须以/结尾
//
//                .baseUrl("http://192.168.1.46:8320/gsapi/")
//
//                //使用Gson作为json数据的转换器
//
//                .addConverterFactory(GsonConverterFactory.create())
//
//                .build();


        //2.创建接口的实现类对象：让retrofit创建一个实例对象

        //Retrofit内部是通过动态代理来创建实例对象，并且监听对象方法的调用;

        //当我们调用业务方法时，Retrofit内部就获取方法的注解信息，这些信息

        //包含的请求方式，url，和请求参数等，于是它会自动的利用okhttp发送这些请求


    }

    private OkHttpClient genericClient() {

        OkHttpClient.Builder localBuilder = new OkHttpClient.Builder();
        localBuilder.connectTimeout(10L, TimeUnit.SECONDS);
        localBuilder.readTimeout(10L, TimeUnit.SECONDS);

        localBuilder.addInterceptor(paramAnonymousChain -> {

            Request localRequest = paramAnonymousChain.request();

            StringBuilder localStringBuilder = new StringBuilder();
            localStringBuilder.append("request url");
            localStringBuilder.append(localRequest.url());

            return paramAnonymousChain.proceed(localRequest.newBuilder()
                    .header("Accept", "application/json")
                    .header("X-CWMAPI-ApiKey", Utils.getApp().getString(R.string.apikey))
                    .header("X-CWMAPI-ApiSecret", Utils.getApp().getString(R.string.apisecret))

                    .build());
        });


        return localBuilder.build();
    }


    /**
     * 单例
     *
     * @return 返回
     */
    public static synchronized CWMRetrofit instance() {

        if (mInstance == null) {
            mInstance = new CWMRetrofit();
        }

        return mInstance;
    }

    /**
     * 动态创建
     *
     * @param clazz clazz
     * @return
     */
    public <T> T create(Class<T> clazz) {

        if (retrofit == null) {
            init();
        }

        return retrofit.create(clazz);
    }
}
